package com.xuecheng.auth.service;

import com.alibaba.fastjson.JSON;

import com.xuecheng.auth.client.UserClient;
import com.xuecheng.auth.dao.UserDao;
import com.xuecheng.auth.model.UserDto;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
public class SpringDataUserDetailsService implements UserDetailsService {

    @Autowired
    UserDao userDao;

    @Autowired
    UserClient userClient;

    //根据 账号查询用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //将来连接数据库根据账号查询用户信息
        UserDto userDto = userDao.getUserByUsername(username);
        if(userDto == null){
            //如果用户查不到，返回null，由provider来抛出异常
            return null;
        }

        //远程调用用户中心根据账号查询用户信息
        XcUserExt userext = userClient.getUserext(username);
        if(userext == null){
            //返回空给spring security表示用户不存在
            return null;
        }

        //根据用户的id查询用户的权限
        List<XcMenu> permissions = userext.getPermissions();
        if(permissions == null){
            permissions = new ArrayList<>();
        }
        List<String> user_permission = new ArrayList<>();
        permissions.forEach(item-> user_permission.add(item.getCode()));
        String user_permission_string  = StringUtils.join(user_permission.toArray(), ",");


//        List<String> permissions = userDao.findPermissionsByUserId(userDto.getId());
//        //将permissions转成数组
//        String[] permissionArray = new String[permissions.size()];
//        permissions.toArray(permissionArray);

        //将userDto转成json
        String principal = JSON.toJSONString(userDto);
//        String principal = JSON.toJSONString(username);

        UserJwt userDetails = new UserJwt(principal,
                userDto.getPassword(),
                AuthorityUtils.commaSeparatedStringToAuthorityList(user_permission_string));

        userDetails.setId(userext.getId());
        userDetails.setUtype(userext.getUtype());//用户类型
        userDetails.setCompanyId("1");//所属企业
        userDetails.setName(userext.getName());//用户名称
        userDetails.setUserpic(userext.getUserpic());//用户头像


//        UserDetails userDetails = User.withUsername(principal).password(userDto.getPassword()).authorities(user_permission_string).build();
        return userDetails;// TODO

//        org.springframework.security.core.userdetails.User userDetails1 = new org.springframework.security.core.userdetails.User(username,
//                 userDto.getPassword(),
////                AuthorityUtils.commaSeparatedStringToAuthorityList(""));
//                AuthorityUtils.createAuthorityList("course_get_baseinfo","course_get_list"));
//        return userDetails;
    }
}
